package com.mall.order.mq.delayorder;

import com.mall.order.OrderCoreService;
import com.mall.order.OrderQueryService;
import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dto.CancelOrderRequest;
import com.mall.order.dto.CancelOrderResponse;
import com.mall.order.dto.OrderGoodsRequest;
import com.mall.order.dto.OrderGoodsResponse;
import com.mall.shopping.constants.ShoppingRetCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.nio.charset.Charset;
import java.util.List;

/**
 * @author : xfeng
 * @time : 2021/8/30 15:55
 * @program: myproject3
 * @description:
 */
@Component
@Slf4j
public class DelayOrderCancelConsumer {

    private DefaultMQPushConsumer consumer;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderCoreService orderCoreService;

    @PostConstruct
    public void init() {
        consumer = new DefaultMQPushConsumer("delay_order_cancel_consumer");
        consumer.setNamesrvAddr("127.0.0.1:9876");
        try {
            consumer.subscribe("delay_order_cancel", "*");
            consumer.setMessageListener(new MessageListenerConcurrently() {
                @Override
                public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                    //在下单之后的半小时之后，消费到这条消息
                    // 1. 从message中获取orderId, 查询订单状态，如果订单的状态是已支付，什么都不做
                    byte[] body = list.get(0).getBody();
                    String orderId = new String(body, 0, body.length, Charset.forName("utf-8"));

                    Order order = orderMapper.selectByPrimaryKey(orderId);
                    Integer orderStatus = order.getStatus();

                    //  3. 判断订单的状态，不是已取消状态，不是支付成功的状态，就取消订单
                    //      a. 修改订单状态
                    //      b. 还原库存
                    //TODO 注意：这里修改了组长OrderMapper的代码，把7改成了5
                    if (orderStatus != 1 && orderStatus != 7) {
                        //取消订单的逻辑
                        CancelOrderRequest cancelOrderRequest = new CancelOrderRequest();
                        cancelOrderRequest.setOrderId(orderId);
                        CancelOrderResponse cancelOrderResponse = orderCoreService.cancelOrder(cancelOrderRequest);
                        if (ShoppingRetCode.SUCCESS.getCode().equals(cancelOrderResponse.getCode())) {
                            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                        }
                        return null;
                    }
                    //如果是支付成功，直接返回
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                }
            });

            consumer.start();

        } catch (MQClientException e) {
            e.printStackTrace();
            log.error(e.getErrorMessage());
        }

    }
}
